/*****************************
 *@file: BaseSummon
 *@author: 陈吕唐
 *@desc: 基础召唤物
 *@date: 2024-03-26	11:35
 *****************************/
import { _decorator, Collider2D, Component, Contact2DType, IPhysics2DContact, warn } from 'cc';
import { BaseCharacter } from '../BaseCharacter';
import IBaseBulletValue from '../bullet/IBaseBulletValue';
import GameUtils from '../GameUtils';
import { PhysicsEnum } from '../PhysicsEnum';
const { ccclass, property } = _decorator;

@ccclass('BaseSummon')
export class BaseSummon extends Component {
    /****************************************************************************************************************
     *Readonly 常量
     ****************************************************************************************************************/

    /****************************************************************************************************************
     *Property 定义变量
     ****************************************************************************************************************/
    /**
     * 子弹伤害数据
     */
    public value: IBaseBulletValue = undefined!;

    @property({ group: { name: 'BOX2D', id: '1' }, type: Collider2D, override: true, tooltip: `当前感应范围形状(默认从当前节点查找)` })
    protected sensorCollider: Collider2D = null;
    /***************************************************************
     *PropertyUI 引用的控件
     ***************************************************************/

    /****************************************************************************************************************
     * Lifecycle 生命周期相关方法
     ****************************************************************************************************************/
    protected onLoad(): void {
        let colliders = this.node.getComponents(Collider2D);
        if (!this.sensorCollider) {
            this.sensorCollider = colliders.find((c) => c.tag === PhysicsEnum.ColliderTagGroup.SENSOR);
        }

        if (this.sensorCollider) {
            this.addSensorListener(this.sensorCollider);
        }
    }
    /****************************************************************************************************************
     * Public 公有方法
     ****************************************************************************************************************/

    /****************************************************************************************************************
     * private 私有方法
     ****************************************************************************************************************/
    /**
     * 进入感应范围时调用
     * @param selfCollider 
     * @param otherCollider 
     * @param contact 
     */
    private onBeginSensorContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        // 只在两个碰撞体开始接触时被调用一次
        let character = GameUtils.getCharacterByCollider(otherCollider);
        if (character) {
            this.onSensor(character);
        }
    }

    /**
     * 退出感应范围时调用
     * @param selfCollider 
     * @param otherCollider 
     * @param contact 
     */
    private onEndSensorContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        // 只在两个碰撞体结束接触时被调用一次
        let character = GameUtils.getCharacterByCollider(otherCollider);
        if (character) {
            this.onLeave(character);
        }
    }

    /**
     * 进入感应范围
     * @param character 
     */
    protected onSensor(character: BaseCharacter) {

    }

    /**
     * 进入感应范围
     * @param character 
     */
    protected onLeave(character: BaseCharacter) {

    }

    /**
     * 添加感应范围监听
     * @param collider 
     * @returns 
     */
    private addSensorListener(collider: Collider2D = this.sensorCollider) {
        if (!collider) {
            warn('collider is null');
            return;
        }

        if (!collider.hasEventListener(Contact2DType.BEGIN_CONTACT, this.onBeginSensorContact, this)) {
            collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginSensorContact, this);
        }
        if (!collider.hasEventListener(Contact2DType.END_CONTACT, this.onEndSensorContact, this)) {
            collider.on(Contact2DType.END_CONTACT, this.onEndSensorContact, this);
        }
    }
    /****************************************************************************************************************
     * Property Get && Set 属性的 get && set 函数
     ****************************************************************************************************************/

}

